<?php

/**
 * 商品分类管理
 * Enter description here ...
 * @author Admin
 *
 */
class CategoryAction extends CommonAction{
	
	
	/**
	 * 分类列表
	 * Enter description here ...
	 */
	public function index() {
		$Category = D('Category');   //商品分类
		
		import("@.ORG.Page"); //导入分页类
		
		$count = $Category->count();    //计算总数
		$p = new Page ($count,10);   //分页构造方法
					  
		$list=$Category->field("id,categoryname,fid,state,path,concat(path,'-',id) as bpath")
			->limit($p->firstRow.','.$p->listRows) 
			->order('bpath')
			->select();		

		foreach ($list as &$value){
			unset($tmp);
			
			if (!empty($value['fid'])){
				$tmp = $Category->find($value['fid']);      //查询父分类ID
			}
			
			$value['fname'] = $tmp['categoryname'];
		}
		
					  
		$page = $p->show();   
		
		$this->assign("page",$page);
		
		$this->assign("categorydata",$list);
		
		$this->display();
	}
	
	
	
	
	/**
	 * 新建分类
	 * @see CommonAction::add()
	 */
	public function add(){
		$Category = D('Category');
		
		//...无限极分类
		$list=$Category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")->order('bpath')->select();
		
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
		}
		
		$this->assign("categorydata",$list);   
		
		parent::add();
	}
	
	
	
	
	
	
	/**
	 * 保存新建分类
	 * @see CommonAction::insert()
	 */
	public function insert(){
		$data = Init_GP(array('categoryname','fid','state'));  //获取页面内容
		
		parent::insert($data);
	}
	
	
	
	
	/**
	 * 修改分类
	 * @see CommonAction::edit()
	 */
	public function edit(){
		$id = GetNum($_GET['id']);  //主键
		
		$Category = D('Category');
		$children = $Category->getChildren($id);  //获取当前分类下属所有子分类  
		
		if (empty($children)){
			$childrenstr = $id;
		}else {
			$childrenstr = implode(',', $children).','.$id;    //implode() 函数把数组元素组合为一个字符串
		}
		
		
		$map['id'] = array('not in',$childrenstr);
		//...无限极分类
		$list=$Category->field("id,categoryname,fid,path,concat(path,'-',id) as bpath")
			->where($map)
			->order('bpath')
			->select();
		
		foreach($list as $key=>$value){
			$list[$key]['count']=count(explode('-',$value['bpath']));
			
			Log::write("..........." .$key  . '   ' .count(explode('-',$value['bpath'])) )			;
		}
		
		$this->assign("categorydata",$list);
		
		parent::edit();
	}
	
	
	
	
	/**
	 * 保存修改分类
	 * @see CommonAction::update()
	 */
	public function update(){
		$where['id'] = GetNum($_POST['id']);  //主键
		$data = Init_GP(array('categoryname','fid','state'));   //获取页面属性
		
		parent::update($data,$where);
	}
	
	
	
	
	
	
	
	
	
}
?>